package com.e9ab98e991ab.moduler.ui.vivo;

import android.accessibilityservice.AccessibilityService;
import android.os.Build;
import android.os.Bundle;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.e9ab98e991ab.libcommon.utils.SPSingleData;
import com.e9ab98e991ab.moduler.R;
import com.google.common.base.Optional;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;


public class InstallHelperService extends AccessibilityService {
    public static final String KEY_PASSWORD = "key_password";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo rootNode = getRootInActiveWindow();
        if (!Optional.of(rootNode).isPresent()) {
            return;
        }

        if ("com.vivo.secime.service".contentEquals(event.getPackageName())) {
            String password = (String) SPSingleData.getObjectForKey(getApplication(), KEY_PASSWORD, "");

            fillPassword(rootNode, Optional.of(password).toString());

        } else if ("com.android.packageinstaller".contentEquals(event.getPackageName())) {
            installConfirm(rootNode);
        }
    }

    private void fillPassword(AccessibilityNodeInfo rootNode, String password) {
        AccessibilityNodeInfo editText = rootNode.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
        if (!Optional.of(editText).isPresent()) {
            return;
        }
        if ("com.bbk.account".contentEquals(editText.getPackageName())
                && "android.widget.EditText".contentEquals(editText.getClassName())) {
            Bundle arguments = new Bundle();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                arguments.putCharSequence(AccessibilityNodeInfo
                        .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, password);
                editText.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
            }
        }

        List<AccessibilityNodeInfo> nodeInfoList = rootNode.findAccessibilityNodeInfosByText(getString(R.string.determine));
        for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
        List<AccessibilityNodeInfo> nodeInfoListInstall = rootNode.findAccessibilityNodeInfosByText(getString(R.string.continue_to_install));
        for (AccessibilityNodeInfo nodeInfo : nodeInfoListInstall) {
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
    }

    private void installConfirm(AccessibilityNodeInfo rootNode) {
        List<AccessibilityNodeInfo> nodeInfoList = new ArrayList<>();
        nodeInfoList.addAll(rootNode.findAccessibilityNodeInfosByText(getString(R.string.continue_to_install)));
        nodeInfoList.addAll(rootNode.findAccessibilityNodeInfosByText(getString(R.string.installation))); 
        nodeInfoList.addAll(rootNode.findAccessibilityNodeInfosByText(getString(R.string.open)));

        for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
    }

    @Override
    public void onInterrupt() {
    }


}
